package com.travelingsalesmen.post.gui;

import com.travelingsalesmen.post.Catalog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

public class ProductPanel extends JPanel {

    private ActionListener addItemListener;

    /**
     * Creates new form ProductPanel
     */
    public ProductPanel() {
        initComponents();
    }

    public void setAddItemListener(ActionListener addItemListener) {
        this.addItemListener = addItemListener;
    }

    public void populateList(Catalog catalog) {
        cmbUPC.removeAllItems();
        cmbQuantity.removeAllItems();
        
        for (String upc : catalog.getUpcs()) {
            cmbUPC.addItem(upc);
        }

        for (int i = 1; i < 10; i++) {
            cmbQuantity.addItem(i);
        }
    }

    public int getQuantity() {
        return Integer.parseInt(cmbQuantity.getItemAt(cmbQuantity.getSelectedIndex()).toString());
    }

    public String getUPC() {
        return cmbUPC.getItemAt(cmbUPC.getSelectedIndex()).toString();
    }

    public void reset() {
        cmbUPC.setSelectedIndex(0);
        cmbQuantity.setSelectedIndex(0);
    }

    /**
     * WARNING: Do NOT modify this code. The content of this method is always
     * This method is called from within the constructor to initialize the form.
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ProductPanel = new javax.swing.JPanel();
        cmbUPC = new javax.swing.JComboBox();
        cmbQuantity = new javax.swing.JComboBox();
        lblQuantity = new javax.swing.JLabel();
        lblProductUPC = new javax.swing.JLabel();
        btnEnter = new javax.swing.JButton();

        ProductPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Product", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        cmbUPC.setName(""); // NOI18N

        lblQuantity.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblQuantity.setText("Quantity");

        lblProductUPC.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblProductUPC.setText("Product (UPC)");

        btnEnter.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnEnter.setText("ENTER");
        btnEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ProductPanelLayout = new javax.swing.GroupLayout(ProductPanel);
        ProductPanel.setLayout(ProductPanelLayout);
        ProductPanelLayout.setHorizontalGroup(
            ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ProductPanelLayout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblProductUPC)
                    .addComponent(lblQuantity))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductPanelLayout.createSequentialGroup()
                        .addComponent(cmbUPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(btnEnter))
                    .addComponent(cmbQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        ProductPanelLayout.setVerticalGroup(
            ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ProductPanelLayout.createSequentialGroup()
                .addGroup(ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblProductUPC)
                            .addComponent(cmbUPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(ProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblQuantity)
                            .addComponent(cmbQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(ProductPanelLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(btnEnter)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ProductPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ProductPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnterActionPerformed
        addItemListener.actionPerformed(evt);
    }//GEN-LAST:event_btnEnterActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ProductPanel;
    private javax.swing.JButton btnEnter;
    private javax.swing.JComboBox cmbQuantity;
    private javax.swing.JComboBox cmbUPC;
    private javax.swing.JLabel lblProductUPC;
    private javax.swing.JLabel lblQuantity;
    // End of variables declaration//GEN-END:variables
}
